import config from '../../config'
const UrlTool = {
		getHref: function () {
			return window.location.href;
		},

		getProtocol: function () {
			return window.location.protocol;
		},
		getHost: function (n) { //获得域名
			const host = window.location.host;
			const hostPart = host.split(':')[0].split('.');
			switch (n) {
				case 1: return hostPart[hostPart.length - 1];
				case 2: return hostPart[hostPart.length - 2];
				case 3: return hostPart[hostPart.length - 3];
				case 4: return hostPart[hostPart.length - 4];
			}
			return window.location.host;
		},
		getPort: function () {
			return window.location.port;
		},
		getPathname: function () {
			return window.location.pathname;
		},
		getSearch: function () {
			return window.location.search;
		},

		getPamas: function () { //获得url的参数 例： http://182.119.92.181:8007/services/CBPSCON/index.html?orgNo=11002766013212711054
			const searchStr = window.location.search;
			const pamas = searchStr.replace('?', '').split('&');
			let pamaMap = {};
			for (let i = 0; i < pamas.length; i++) {
				const temp = pamas[i].split('=');
				if (temp[0] === '') continue;
				pamaMap[temp[0]] = temp[1];
			}
			return pamaMap;
		},

		getPamasByHash: function () { //获得url的参数 hash方式 例：http://182.119.92.181:8007/services/CBPSCON/index.html#11002766013212711054
			const searchStr = window.location.hash;
			const pamas = searchStr.replace('#', '').split('&');
			let pamaMap = {};
			for (let i = 0; i < pamas.length; i++) {
				const temp = pamas[i].split('=');
				if (temp[0] === '') continue;
				pamaMap[temp[0]] = temp[1];
			}
			return pamaMap;
		},


    getUrl(url){
      if (process.env.NODE_ENV === 'development') {
        if (config.devComm.proxy === true && (url.startsWith(`/portal/${config.systemCode}`) || url.startsWith(`/apis/${config.systemCode}`))) {
          url = config.devComm.urlPrefix + url.replace(`/${config.systemCode}/`,'/')
        }
      } else if (config.urlPrefix) {
        for (const prefix of config.urlPrefix) {
          if (window.location.pathname.indexOf(prefix) !== -1 && url.indexOf(prefix) === -1) {
            const prefixSub = prefix.substring(0, prefix.length - 1)
            url = `${prefixSub}${url}`
            break
          }
        }
      }
      return url
    },
  downloadImage: function (fileName) { // 图片下载---url
    return UrlTool.getUrl('/apis/iifs-edu-service-base/doc/' + fileName)
  },

	}

export default UrlTool;
